#coding:utf-8
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
import os


#连接设备(等待时间，‘版本号’)
device = MonkeyRunner.waitForConnection(5,'650e970b7d93')


device.press('KEYCODE_HOME',MonkeyDevice.DOWN_AND_UP)

#打开王者传奇，等待几秒 

runComponent = "com.shenghe.wzcq.mi/.FirstActivity"
device.startActivity(component=runComponent)

MonkeyRunner.sleep(5)


#根据颜色，判断是否进入游戏

result2 = device.takeSnapshot()
result2=result2.getRawPixelInt(160,599)
print(result2)

while result2 != -13360880 :
    MonkeyRunner.sleep(1)
    result2 = device.takeSnapshot()
    result2=result2.getRawPixelInt(160,599)
    print(result2)
else:
    MonkeyRunner.sleep(3)
    device.touch(1035,347,'DOWN_AND_UP')
    print('ok1')
#等待5秒
MonkeyRunner.sleep(6)
device.touch(1189,103,'DOWN_AND_UP')
device.touch(1189,103,'DOWN_AND_UP')
print('ok2')
MonkeyRunner.sleep(3)
#取消

device.touch(1002,172,'DOWN_AND_UP')
print('ok3')
MonkeyRunner.sleep(3)

device.touch(765,600,'DOWN_AND_UP')
MonkeyRunner.sleep(3)



#截图 A tuple (x, y, w, h)，x，y指定矩形区域的左上角，w为矩形宽，h为矩形高
result = device.takeSnapshot()
result= result.getSubImage((470,309,108,30))
result.writeToFile('D:\\python_file\\monkeyrunner\\test4.png','png')


# os.system("python D:\python_file\ocr_test\png.py")
code1=os.popen("python D:\python_file\ocr_test\png.py")
print(code1.read())


'''
#选区
while code1.read().encode('utf-8')!= '王耆95区'.encode('utf-8'):
    device.touch(645,465,'DOWN_AND_UP')
    device.drag((645,465),(645,400),1)
    MonkeyRunner.sleep(0.2)
    result = device.takeSnapshot()
    result= result.getSubImage((470,309,108,30))
    result.writeToFile('D:\\python_file\\monkeyrunner\\test4.png','png')
    code1=os.popen("python D:\python_file\ocr_test\png.py")
    print(code1.read())
else:
    device.touch(区服,'DOWN_AND_UP')
    device.touch(进入游戏,'DOWN_AND_UP')

'''
#进入游戏
MonkeyRunner.sleep(2)
device.touch(1164,638,'DOWN_AND_UP')
#进入游戏
MonkeyRunner.sleep(2)
device.touch(1150,655,'DOWN_AND_UP')

#点击背包
MonkeyRunner.sleep(5)
device.touch(900,663,'DOWN_AND_UP')
MonkeyRunner.sleep(0.5)

#截图元宝
result1 = device.takeSnapshot()
result1= result1.getSubImage((665,595,160,30))
result1.writeToFile('D:\\python_file\\monkeyrunner\\test4.png','png')
code1=os.popen("python D:\python_file\ocr_test\yb1.py")
print(code1.read())

device.touch(966,467,'DOWN_AND_UP')


